#ifndef __sys_h__
#define __sys_h__ 1

#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <pthread.h>
#include <assert.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/queue.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/sysinfo.h>
#include <linux/errno.h>
// #include <linux/kernel.h>

#include "compat-android.h"

#define __LINUX_OS__  (defined(__linux__) && !defined(__ANDROID__))

#define PROC_CPU_INFO "/proc/cpuinfo"

#define MAX_VAL(a, b) (a) > (b) ? (a) : (b)
#define MIN_VAL(a, b) (a) < (b) ? (a) : (b)

#ifdef timeradd
#undef timeradd
#endif
#define timeradd(tvp1, tvp2, tvp3)                             \
    do {                                                       \
        (tvp3)->tv_sec = (tvp2)->tv_sec + (tvp1)->tv_sec;      \
        (tvp3)->tv_usec = (tvp2)->tv_usec + (tvp1)->tv_usec;   \
        if ((tvp3)->tv_usec > (1000 * 1000)) {                 \
            (tvp3)->tv_sec += (tvp3)->tv_usec / (1000 * 1000); \
            (tvp3)->tv_usec = (tvp3)->tv_usec % (1000 * 1000); \
        }                                                      \
    } while (0)

#ifdef timersub
#undef timersub
#endif
#define timersub(tvp1, tvp2, tvp3)                                                     \
    do {                                                                               \
        if ((tvp1)->tv_usec >= (tvp2)->tv_usec) {                                      \
            (tvp3)->tv_usec = (tvp1)->tv_usec - (tvp2)->tv_usec;                       \
        } else {                                                                       \
            (tvp3)->tv_usec = ((tvp1)->tv_usec + (1 * 1000 * 1000)) - (tvp2)->tv_usec; \
            (tvp1)->tv_sec--;                                                          \
        }                                                                              \
        (tvp3)->tv_sec = (tvp1)->tv_sec - (tvp2)->tv_sec;                              \
        (tvp3)->tv_sec = (tvp3)->tv_sec < 0 ? 0 : (tvp3)->tv_sec;                      \
    } while (0)


#define Array_Size(arr) (sizeof(arr)/sizeof(arr[0]))

#define Positive(d) ((d) < 0 ? -(d) : (d))

#define SEC2MSEC(s) ((s)*1000)
#define MSEC2USEC(s) ((s)*1000)
#define MSEC2SEC(s)     ((s)/1000)

#define _offset(type,member)    ((long int)&((type *)0)->member)


void get_timestring(char *buf);
#endif
